/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.client.plugin;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.edi.Kola;
import com.edi.client.param.EdiClientParam;
import com.edi.client.param.EdiServerParam;
import com.edi.manager.PluginManager;
import com.edi.manager.ServiceManager;
/**
 * 标准插件 例如jar包插件...
 * @author cya
 *
 */
public class StandardPlugin implements Plugin {
	private static final Log log = LogFactory.getLog(StandardPlugin.class);
	private String name;
	private String description;
	private String author;
	private String version;
	private String date;
	private String ediServerVersion;
	private List<Plugin> scriptPlugins=new ArrayList<Plugin>();
	private List<Plugin> serverPlugins=new ArrayList<Plugin>();
	private File pluginDir;

	public void dispose() {  
		PluginManager pluginManager = Kola.getPluginManager();
		ServiceManager serviceManager = Kola.getServiceManager();
		Iterator<Plugin> it=scriptPlugins.iterator();
		while(it.hasNext()){
			Plugin plugin=it.next();
			pluginManager.removeScriptPlugin(plugin.getName());
			it.remove();
		}
		Iterator<Plugin> serit=serverPlugins.iterator();
		while(serit.hasNext()){
			Plugin plugin=serit.next();
			plugin.dispose();
			serit.remove();
		}
		Iterator<String> keyIt=serviceManager.getEdiServices().keySet().iterator();
		while(keyIt.hasNext()){
			String key=keyIt.next();
			log.debug("key ="+key);
			log.debug("name ="+name);
			if(StringUtils.startsWithIgnoreCase(key, name+":")){ 
				keyIt.remove();
			}
		}		
		pluginManager.removeStandardPlugin(name);
		pluginManager.removeClassLoader(name);
	}

	public String getName() { 
		return name;
	}

	public void init() {  
	}

	public PluginResult invoke(EdiClientParam param) throws Exception { 
		return null;
	}

	public PluginResult invoke(EdiServerParam param) throws Exception { 
		return null;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getDate() {
		return date;
	}

	public void setDate(String date) {
		this.date = date;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getEdiServerVersion() {
		return ediServerVersion;
	}

	public void setEdiServerVersion(String ediServerVersion) {
		this.ediServerVersion = ediServerVersion;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public void setName(String name) {
		this.name = name;
	}
	public void addScriptPlugin(Plugin plugin){
		this.scriptPlugins.add(plugin);
	}

	public File getPluginDir() {
		return pluginDir;
	}

	public void setPluginDir(File pluginDir) {
		this.pluginDir = pluginDir;
	}
	

}
